#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
/*
 * Copyright © 2014, Finium Sdn Bhd, All Rights Reserved
 * 
 * AbstractHibernateQueryUtil.java
 * Modification History
 * *************************************************************
 * Date				Author						Comment
 * Oct 26, 2013		Venkaiah Chowdary Koneru	Created
 * *************************************************************
 */
package ${package}.support.hibernate;

import java.util.Map;
import java.util.Map.Entry;

import ${package}.dto.ListProp;

import org.hibernate.Query;
import org.springframework.util.StringUtils;

/**
 * Utility class to hold generic HQL/Named Queries related functionality.
 * 
 * @author Venkaiah Chowdary Koneru
 */
public abstract class AbstractHibernateQueryUtil extends AbstractHibernateUtil
{
    /**
     * Set parameters value into the query.
     * 
     * @param parameters
     *            Map containing key (i.e. key of the parameter in the query,
     *            e.g. :key1 written in the hql).
     * @param query
     *            result query object.
     */
    protected static void setParameters(Map<String, Object> parameters,
            Query query)
    {
        if ((parameters == null) || (query == null))
        {
            return;
        }

        for (Entry<String, Object> parameter : parameters.entrySet())
        {
            query.setParameter(parameter.getKey(), parameter.getValue());
        }
    }

    /**
     * Append sorting property and ascending or descending value.
     * 
     * @param listProp
     *            listProp object.
     * @param aliasAndPropertyMap
     *            alias and the associated property name map. This field is
     *            optional. if it is null or not containing the property for the
     *            sorting, then the sorting.property value will be used.
     * 
     * @return String after append the property and asc/desc.
     */
    protected static String generateSorting(ListProp listProp,
            Map<String, String> aliasAndPropertyMap)
    {
        if (listProp == null
                || !StringUtils.hasText(listProp.getSortProperty()))
        {
            return "";
        }

        String sortProperty = listProp.getSortProperty();

        if (aliasAndPropertyMap != null
                && aliasAndPropertyMap.containsKey(sortProperty))
        {
            sortProperty = aliasAndPropertyMap.get(sortProperty);
        }

        return " order by " + sortProperty
                + (listProp.isSortAscending() ? " asc " : " desc ");
    }

    /**
     * Set pagination values into the query object.
     * 
     * also calculates the number of pages.
     * 
     * @param listProp
     *            listProp object.
     * @param query
     *            pagination values will be set into this query object.
     */
    protected static void setPagination(ListProp listProp, Query query)
    {
        if (listProp == null || query == null
                || !(listProp.getStartIndex() > getStartIndex()))
        {
            return;
        }

        if (updatePagination(listProp))
        {
            query.setFirstResult((int) listProp.getStartIndex());
            query.setMaxResults((int) (listProp.getEndIndex()
                    - listProp.getStartIndex() + 1));
        }
    }
}
